#!/bin/sh

case $1 in -h|--help)
	echo '
password box

 USAGE:
  box_passwd [options] "title" "frame_title" "text1" "text2" "text3"

options:
  --show             : show password
  --default "passwd" : default password string
  --ok-label "str"   : label for the Ok button
  --show-label       : label for "show password" checkbox

 all arguments are optional
'
	exit ;;
esac

export TEXTDOMAIN=box_passwd
export OUTPUT_CHARSET=UTF-8

TITLE="$(gettext 'Password')"
TEXT="$(gettext 'Enter password')"
SHOWP_TEXT="$(gettext 'Show password')"
OK_LABEL="$(gettext 'OK')"
DEFAULT_PWD=""

for i in $@ ; do
	case $1 in
		--show) SHOW_PASSWORD=1 ; shift ;;
		--ok-label) OK_LABEL=$2 ; shift 2 ;;
		--show-label) SHOWP_TEXT=$2 ; shift 2 ;;
		--default)
			DEFAULT_PWD='<default>'"${2}"'</default>'
			shift 2
			;;
		--cancel) SHOW_CANCEL_BUTTON=1 ; shift ;;
		*) break ;;
	esac
done

if [ $SHOW_PASSWORD ] ; then
	CHK_STATE='true'
	FOCUS='PASS_SHOWN'
	T='
      <vbox  space-expand="true" space-fill="true">
        <entry caps-lock-warning="true" visibility="true">
          <variable>PASS_SHOWN</variable>
          '${DEFAULT_PWD}'
          <input>echo "$PASS_HIDDEN"</input>
          <action>refresh:PASS_HIDDEN</action>
        </entry>
      </vbox>
      <vbox space-expand="true" space-fill="true">
        <entry caps-lock-warning="true" visibility="false">
          <variable>PASS_HIDDEN</variable>
          '${DEFAULT_PWD}'
          <input>echo "$PASS_SHOWN"</input>
          <action>refresh:PASS_SHOWN</action>
        </entry>
      </vbox>
'
else
	CHK_STATE='false'
	FOCUS='PASS_HIDDEN'
	T='
      <vbox space-expand="true" space-fill="true">
        <entry caps-lock-warning="true" visibility="false">
          <variable>PASS_HIDDEN</variable>
          '${DEFAULT_PWD}'
          <input>echo "$PASS_SHOWN"</input>
          <action>refresh:PASS_SHOWN</action>
        </entry>
      </vbox>
      <vbox  space-expand="true" space-fill="true">
        <entry caps-lock-warning="true" visibility="true">
          <variable>PASS_SHOWN</variable>
          '${DEFAULT_PWD}'
          <input>echo "$PASS_HIDDEN"</input>
          <action>refresh:PASS_HIDDEN</action>
        </entry>
      </vbox>
'
fi

if [ $SHOW_CANCEL_BUTTON ] ; then
	CANCEL_BUTTON='
      <button>
         '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
         <label>"'$(gettext 'Cancel')'"</label>
       </button>
'
fi

[ "$1" ] && TITLE="$1"
[ "$2" ] && TEXT="$2"

#=================================================================

S='
<window title="'${TITLE}'" image-name="/usr/share/pixmaps/puppy/file_lock.svg">
  <vbox>
   <frame '${TEXT}'>'
     [ "$3" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$3'"</label></text>'
     [ "$4" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$4'"</label></text>'
     [ "$5" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$5'"</label></text>'

S=$S'<notebook show-tabs="false" show-border="false" visible="false">
      '${T}'
      <variable>SWITCH</variable>
      <input>echo $(( (SWITCH+1) &1 ))</input>
    </notebook>
    <checkbox>
      <default>'${CHK_STATE}'</default>
      <label>"'${SHOWP_TEXT}'"</label>
      <action>refresh:SWITCH</action>
    </checkbox>

   </frame>

   <hbox space-expand="false" space-fill="false">
      <button>
         '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
         <label>"'${OK_LABEL}'"</label>
       </button>
       '${CANCEL_BUTTON}'
   </hbox>

  </vbox>

  <action signal="map-event">show:SWITCH</action>
  <action signal="map-event">grabfocus:'${FOCUS}'</action>
</window>'

export MAIN="$S"
eval "`gtkdialog --center -p MAIN`"
if [ "$EXIT" = "OK" ] ; then
	if [ "$PASS_HIDDEN" != "" ] ; then
		echo "$PASS_HIDDEN"
		exit 0
	fi
fi

[ "$EXIT" = "Cancel" ] && exit 2
exit 1

### END ###
